<?php
$StartStatTime = $StatStart;

//this neat little statement tells us, assuming correct spelling and proper input, approximately how many unique albums have been played this week; we'll need this to calculate frequency of each album's play.
$total_plays = mysql_fetch_array(mysql_query("SELECT COUNT(`album`) FROM `airplay`  WHERE `timestamp`>'$StatStartTime' "));
$unique_album_plays = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT LOWER(`album`)) FROM `airplay` WHERE `timestamp`>'$StatStartTime' "));
$total_new_plays = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT `cddbid`) FROM `airplay` WHERE `status`='2' AND `cddbid`!='' AND `timestamp`>'$StatStartTime' "));

//this statement will grab all the CDs that we care about, new ones... we wont be charting anything else, obviously
$stat_query = "SELECT * FROM `airplay` WHERE  `status`<='2' AND `timestamp`>'$StartStatTime' AND `cddbid`!='' ORDER BY `cddbid` DESC ";
$stat_result = mysql_query($stat_query);

$max = mysql_num_rows($stat_result);

$last_id = 0;
$q = 0;
for ($i = 0; $i < $max; $i++) {
	$tmp = mysql_fetch_array($stat_result);
	if ($tmp[cddbid] !== $last_id) {
      $statistics[$q] = mysql_fetch_array($stat_result);
      $statistics[$q][spins] = 1;
      $last_id = $statistics[$q][cddbid];
      $q++;
   } else {
      $statistics[$q - 1][spins]++;
   }
}

foreach ($statistics as $key => $row) {
	$spins[$key] = $row[spins];
}
array_multisort($spins, SORT_DESC, $statistics);

// this will print out everything
for ($i = 0; $i < count($statistics); $i++) {
//	echo($statistics[$i][album] . ' ('. $statistics[$i][spins] . ') #' . $statistics[$i][cddbid] . '<br />');
}
//end printing out








/*  recently commented-out

$rating = 0;
$statistics_query = "SELECT * FROM `temp_stat` ORDER BY `spins` DESC";
$statistics_result = mysql_query($statistics_query) or die(mysql_error());
while ($statistics_row = mysql_fetch_assoc($statistics_result)) {
++$rating;
if (($rating - 1) <= ($total_new_plays[0] / 3)) {
	$heavy_spins = $statistics_row[spins];
} elseif (($rating - 1) <= ($total_new_plays[0] / 1.5)) {
	$medium_spins = $statistics_row[spins];
} 
}

*/  

?>